package com.epcs.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.tagext.TryCatchFinally;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.eclipse.jdt.internal.compiler.ast.TryStatement;

import com.epcs.model.bo.FollowingBO;
import com.epcs.model.bo.NotificationBO;
import com.epcs.model.dao.UserDAO;
import com.epcs.model.dto.Account;
import com.epcs.model.dto.Following;
import com.epcs.model.dto.FollowingList;
import com.epcs.model.dto.Notification;
import com.epcs.model.dto.NotificationList;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Path("check")
public class WebService {
//load notification 0 và 1"mới load page"
	@Path("/loadNotification/{username}")
	@GET
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public String getCheck(@PathParam("username") String user) {
		// Register Gson
		Gson gson = new Gson();
		NotificationBO notifyBO = new NotificationBO();
		UserDAO username = new UserDAO();
		FollowingBO followingBo = new FollowingBO();

		// Load List Following dua tren username
		List<Following> followingList = followingBo.loadAllNotification(username.loadUser(user));
		// Duyet List Following

		// FollowingList resultList = new FollowingList(followingList);
		List<Notification> notify = new ArrayList<Notification>();

		NotificationList list = null;
		if (followingList != null) {
			List<Notification> notificationList = notifyBO.loadAllNotification1(followingList);
			if(notificationList!=null){
			notify.addAll(notificationList);
			list = new NotificationList(notify);
			list.setUsername(user);
			}
		}
	
		return gson.toJson(list);

	}
// get notification có status=0.. notify này vừa đc tạo
	@Path("/getUnloadNotification/{username}")
	@GET
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public String getUnloadNotification(@PathParam("username") String user) {
		// Register Gson
		Gson gson = new Gson();
		NotificationBO notifyBO = new NotificationBO();
		UserDAO username = new UserDAO();
		FollowingBO followingBo = new FollowingBO();

		// Load List Following dua tren username
		List<Following> followingList = followingBo
				.loadAllNotification(username.loadUser(user));
		// Duyet List Following

		List<Notification> notify = new ArrayList<Notification>();

		NotificationList list = null;
		if (followingList != null) {
			List<Notification> notificationList = notifyBO.loadUnloadNotification(followingList);
			if(notificationList!=null ){
			notify.addAll(notificationList);
			list = new NotificationList(notify);
			list.setUsername(user);
			}
		}
		return gson.toJson(list);

	}
// Sau khi load thành công, chuyển status từ 0 thành 1
	@Path("/successNotification/{data}")
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public void Success(@PathParam("data") String data) {
		// Khởi tạo de tui thu cai nay
		NotificationBO notifyBO = new NotificationBO();
		int temp = Integer.parseInt(data);
		notifyBO.updateNotification(temp);
	}
}
